package com.example.servlet;

import com.example.dao.StudentMapper;
import com.example.utils.MybatisUtils;
import org.apache.ibatis.session.SqlSession;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class DeleteStudentServlet extends HttpServlet {

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");

        // Get student ID to delete
        int stu_id =Integer.parseInt(request.getParameter("stu_id")) ;

        // Delete student from database
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);

        studentMapper.deleteStudentById(stu_id);
        sqlSession.commit();
        sqlSession.close();

        // Redirect to a confirmation page or any other page
        response.sendRedirect("home.jsp");
    }
}
